<?php
namespace app\index\controller;
use app\index\controller\Home;
use think\Request;
use think\Session;
class Card extends Home
{
	/**
	 * 跳转到 发表新帖 页面
	 * @return \think\response\View
	 */
	public function newCard ()
	{
		$this->isLogin();
		$this->assign("title","发表新帖");
		
		//查询所有的帖子分类
    	$cate = model("category");
    	$cateInfo = $cate->queryAllCate(['id','category_name']);
    	$this->assign('cateInfo',$cateInfo);

    	//查询当前用户的金币数
    	$user = model("user");
    	$email = input("session.home.email");
    	$userInfo = $user->selectUser(["email" => $email]);
		$this->assign('userInfo',$userInfo);
		return view("Card/newCard");
	}

    /**
     * 发帖
     */
	public function send ()
	{
		if(Request()->isPost()){
//			dump(input("post."));
			//判断帖子内容是否为空
			if(input("post.content1") == ""){
				$this->error("帖子内容不能为空");
			}
			//获取数据
			$category = input("post.class");	//帖子分类
			$name = input("post.title");    	//帖子名
			$content = input("post.content1");  //帖子内容
			$code = input("post.vercode");		//验证码
			$user_id = session("home.id");

			
			//判读验证码是否正确
    		if(!$this->checkVerify($code)){
    			//如果验证码不正确,就把内容里面的图片删除掉
                $this->deleteImg($content);
    			$this->error("验证码错误");
    		}

			//是提问帖
			if($category == 1){
				$gold = input("post.gold");
				$data['gold'] = $gold; //提问贴的酬金
				$data['sort'] = 1;	   //提问帖
				$data['is_end'] = 1;	   //提问帖 未完结
			}else{
				$data['sort'] = 0;     //其他贴
				$data['is_end'] = 0;	   //其他贴
			}
			$data['user_id'] = $user_id; //发帖人id
			$data['name'] = $name;       //帖子名
			$data['content'] = $content; //帖子内容
			$data['category_id'] = $category; //帖子分类
			$data['create_time'] = date("Y-m-d H:i:s");
//			dump($data);

			$card = model("card");
			$id = $card -> saveCard($data);
			if($id > 0){
                //发帖成功 如果是提问帖,就把发帖人的金币 - 该提问帖的酬金
                if($category == 1){
                    $user = model("user");
                    $user->setDecUser("id=$user_id","gold",input("post.gold"));
                }
				$this->success("发帖成功,现在跳转到主页",url("Index/index"));
			}else{
				//如果发帖失败,就把内容里面的图片删除掉
                $this->deleteImg($content);
				$this->error("发帖失败");
			}
		}
	}

    /**
     * 帖子访问量自增1
     */
    public function cardBrowse ()
    {
        $card_id = Request::instance()->param("id");
        $card = model("card");
        //查看帖子的同时,该帖子的浏览量自增1
        $card -> setIncCard("id=$card_id","browse",1);
        $this->redirect('Card/detail', ['id' => $card_id]);
    }

	/**
	 * 跳转到 帖子详情 页面
	 * @return \think\response\View
	 */
	public function detail ()
	{
		$this->assign("title","帖子详情");

        //获取帖子id
        $card_id = Request::instance()->param("id");
        //通过帖子id 查看帖子详情
        $card = model("card");
        $info = $card->queryCardByid($card_id);
//        dump($info);die;
        $this->assign("cardInfo",$info);

        //通过帖子id查询评论
        $eval = model("evaluate");
        $evalList = $eval -> queryAllEvaluateByCardId($card_id);
        $this->assign("evalList",$evalList['data']);
        $this->assign("evalPage",$evalList['page']);
		return view("Card/detail");
	}

    /**
     * 删除内容的图片文件
     * @param $content
     */
    public function deleteImg ($content)
    {
        $regex = '/<img src="(.*?)" title="(.*?)"/';
        preg_match_all($regex, $content, $arr);
        if(!empty($arr)){
            foreach($arr[2] as $key => $val){
                $url = get_img_url($_SERVER['DOCUMENT_ROOT']."/fly_forum/public/upload/image",$val);
                @unlink($url);
            }
        }
    }

    /**
     * 评论
     */
    public function reply ()
    {
        if(Request::instance()->isPost()){
//            dump(input("post."));die;

            //判断帖子内容是否为空
            if(input("post.content1") == ""){
                $this->error("帖子内容不能为空");
            }

            $content = input("post.content1");  //帖子内容
            if(empty(session("home"))){
                //如果未登录,则把内容中的图片从目录中删去
                $this->deleteImg($content);
                $this->error("请先登录","User/login");
            }

            //获取数据
            $card_id = input("post.card_id"); //帖子id
            $user_id = session("home.id");

            $data['user_id'] = $user_id;
            $data['card_id'] = $card_id;
            $data['eval_content'] = $content;
            $data['create_time'] = date("Y-m-d H:i:s");

            $eval = model("evaluate");
            $result = $eval -> addEvaluate($data);
            if($result > 0){
                //评论发表成功后,帖子的评论数自加1
                $card = model("card");
                $card->setIncCard("id=$card_id","evaluate_num",1);
                $this->redirect('Card/detail', ['id' => $card_id]);
            }else{
                $this->error("评论发表失败");
            }






        }

    }
}
?>